package org.wyf.EveryPoint.crowd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.wyf.EveryPoint.common.annotation.ControllerEndpoint;
import org.wyf.EveryPoint.common.controller.BaseController;
import org.wyf.EveryPoint.common.entity.FebsConstant;
import org.wyf.EveryPoint.common.entity.FebsResponse;
import org.wyf.EveryPoint.common.entity.QueryRequest;
import org.wyf.EveryPoint.common.utils.FebsUtil;
import org.wyf.EveryPoint.crowd.entity.CollectionProject;
import org.wyf.EveryPoint.crowd.service.ICollectionProjectService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 用户收藏表 Controller
 *
 * @author wyf
 * @date 2021-04-26 16:43:18
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class CollectionProjectController extends BaseController {

    private final ICollectionProjectService collectionProjectService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "collectionProject")
    public String collectionProjectIndex(){
        return FebsUtil.view("collectionProject/collectionProject");
    }

    @GetMapping("collectionProject")
    @ResponseBody
    @RequiresPermissions("collectionProject:list")
    public FebsResponse getAllCollectionProjects(CollectionProject collectionProject) {
        return new FebsResponse().success().data(collectionProjectService.findCollectionProjects(collectionProject));
    }

    @GetMapping("collectionProject/list")
    @ResponseBody
    @RequiresPermissions("collectionProject:list")
    public FebsResponse collectionProjectList(QueryRequest request, CollectionProject collectionProject) {
        Map<String, Object> dataTable = getDataTable(this.collectionProjectService.findCollectionProjects(request, collectionProject));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增CollectionProject", exceptionMessage = "新增CollectionProject失败")
    @PostMapping("collectionProject")
    @ResponseBody
    @RequiresPermissions("collectionProject:add")
    public FebsResponse addCollectionProject(@Valid CollectionProject collectionProject) {
        this.collectionProjectService.createCollectionProject(collectionProject);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除CollectionProject", exceptionMessage = "删除CollectionProject失败")
    @GetMapping("collectionProject/delete")
    @ResponseBody
    @RequiresPermissions("collectionProject:delete")
    public FebsResponse deleteCollectionProject(CollectionProject collectionProject) {
        this.collectionProjectService.deleteCollectionProject(collectionProject);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改CollectionProject", exceptionMessage = "修改CollectionProject失败")
    @PostMapping("collectionProject/update")
    @ResponseBody
    @RequiresPermissions("collectionProject:update")
    public FebsResponse updateCollectionProject(CollectionProject collectionProject) {
        this.collectionProjectService.updateCollectionProject(collectionProject);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改CollectionProject", exceptionMessage = "导出Excel失败")
    @PostMapping("collectionProject/excel")
    @ResponseBody
    @RequiresPermissions("collectionProject:export")
    public void export(QueryRequest queryRequest, CollectionProject collectionProject, HttpServletResponse response) {
        List<CollectionProject> collectionProjects = this.collectionProjectService.findCollectionProjects(queryRequest, collectionProject).getRecords();
        ExcelKit.$Export(CollectionProject.class, response).downXlsx(collectionProjects, false);
    }


    /**
     * 用户添加收藏
     * @param projectId
     * @return
     */
    @PostMapping("/web/saveUserCollectionProject")
    @ResponseBody
    public FebsResponse saveUserCollectionProject(Long projectId){
        QueryWrapper<CollectionProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",projectId).eq("uid",getCurrentUser().getUserId());
        List<CollectionProject> list = collectionProjectService.list(queryWrapper);
        if (list.size() > 0){
            return new FebsResponse().fail().message("已收藏,请勿重复收藏");
        }
        CollectionProject collectionProject = new CollectionProject();
        collectionProject.setPid(projectId);
        collectionProject.setUid(getCurrentUser().getUserId());
        collectionProjectService.save(collectionProject);
        return new FebsResponse().success().message("收藏成功");
    }
    /**
     * 用户删除收藏
     * @param projectId
     * @return
     */
    @PostMapping("/web/deleteUserCollectionProject")
    @ResponseBody
    public FebsResponse deleteUserCollectionProject(Long projectId){

        QueryWrapper<CollectionProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",projectId).eq("uid",getCurrentUser().getUserId());
        List<CollectionProject> list = collectionProjectService.list(queryWrapper);
        if (list.size() <= 0){
            return new FebsResponse().fail().message("已删除，请勿重复点击");
        }
        collectionProjectService.deleteUserCollectionProject(projectId,getCurrentUser().getUserId());
        return new FebsResponse().success().message("删除成功");
    }
    @PostMapping("/webstUserCollectionProject")
    @ResponseBody
    public FebsResponse listUserCollectionProject(){
        CollectionProject collection = new CollectionProject();
        collection.setUid(getCurrentUser().getUserId());
        List<CollectionProject> list = collectionProjectService.findCollectionProjects(collection);
        return new FebsResponse().success().data(list);
    }
}
